package com.mew.sys;

import com.mew.common.dc.db.BulletinService;
import com.mew.common.dc.redis.UserProfileRedisService;
import com.mew.common.enm.UserType;
import com.mew.common.entity.sys.Bulletin;
import com.mew.common.entity.user.Profile;
import com.mew.common.entity.user.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Controller
@RequestMapping("sys")
public class BulletinController {

    @Autowired
    private BulletinService bulletinService;
    @Autowired
    private UserProfileRedisService userProfileRedisService;

    @RequestMapping("/bulletins/{uid}")
    @ResponseBody
    public List<Bulletin> getBulletins(@RequestParam("uid")long uid){

        List<Bulletin> bulletins = bulletinService.getRedisBulletins();

        User user = userProfileRedisService.getRedisUserById(uid);
        Profile profile = userProfileRedisService.getRedisProfileById(uid);
        ArrayList<Bulletin> list = new ArrayList<>();

        for (Bulletin bulletin : bulletins) {
            Integer vipLevel = profile.getVipLevel();
            if ((bulletin.getFromVip()<=vipLevel && bulletin.getToVip()>=vipLevel)||(user.getUserType()== UserType.Customer.code()&&(bulletin.getFromVip()==99||bulletin.getToVip()==99))){
                list.add(bulletin);
            }
        }
        Collections.sort(list,(a,b)->b.getLayer().compareTo(a.getLayer()));
        return list;
    }
}
